import { getRequest } from './api'

export const initMenu = (router, store) => {
  if (store.state.length > 0) {
    console.log(store.state)
    return
  }
  // getRequest('/system/config/menu').then(data => {
  //   if (data) {
  //     // 格式化Router
  //     let fmtRoutes = formatRoutes(data)
  //     // 添加到router
  //     router.addRoutes(fmtRoutes)
  //     // 将数据存入Vuex
  //     store.commit('initRoutes', fmtRoutes)
  //   }
  // })
}

export const formatRoutes = (routes) => {
  let fmtRoutes = []
  routes.forEach(router => {
    let {
      path,
      component,
      name,
      iconCls,
      children
    } = router
    if (children && children instanceof Array) {
      // 递归
      children = formatRoutes(children)
    }
    let fmRouter = {
      path: path,
      name: name,
      iconCls: iconCls,
      children: children,
      component (resolve) {
        if (component.startsWidth('Emp')) {
          require(['../views/emp/' + component + '.vue'],resolve)
        } else if (component.startsWidth('Per')) {
          require(['../views/per/' + component + '.vue'],resolve)
        } else if (component.startsWidth('')) {

        }
      }
    }
    fmtRoutes.push(fmRouter)
  })
  return fmtRoutes
}